package com.zzsy.shoppingpro.type.framgent;

import android.view.View;
import android.widget.GridView;

import com.zzsy.shoppingpro.R;
import com.zzsy.shoppingpro.app.Config;
import com.zzsy.shoppingpro.base.BaseFragment;
import com.zzsy.shoppingpro.basebean.BaseBean;
import com.zzsy.shoppingpro.http.BaseCallback;
import com.zzsy.shoppingpro.http.RetrofitHelper;
import com.zzsy.shoppingpro.server.MainService;
import com.zzsy.shoppingpro.type.bean.TagBean;
import com.zzsy.shoppingpro.type.customer.SearchTipsGroupView;
import com.zzsy.shoppingpro.utils.Toast;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2019/8/20 0020.
 */

public class TagFragment extends BaseFragment {
    private SearchTipsGroupView tipsGroupView;


    @Override
    public View initView() {
        View view = View.inflate(context, R.layout.fragment_tag, null);
        tipsGroupView = view.findViewById(R.id.search_id);
        return view;
    }

    /**
     * 网络请求数据
     */
    @Override
    public void initData() {

        RetrofitHelper.create(MainService.class, Config.BASE_URL_JSON)
                .getTagData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseCallback<BaseBean<List<TagBean>>>() {
                    @Override
                    public void success(BaseBean<List<TagBean>> listBaseBean) {
                        setData(listBaseBean);

                    }

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 请求数据接口
     * */
    private void setData(BaseBean<List<TagBean>> listBaseBean) {
        String[] strArr = new String[listBaseBean.getData().size()];

        for (int i = 0; i < listBaseBean.getData().size(); i++) {
            strArr[i] = listBaseBean.getData().get(i).getName();
        }
        tipsGroupView.initViews(strArr);
        tipsGroupView.setOnItemClickListener(new SearchTipsGroupView.onItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.show(context,listBaseBean.getData().get(position).getName());
            }
        });
    }
}
